import axios from 'axios';
import * as cheerio from 'cheerio';
import { URL } from 'url';

// 生成随机的User-Agent
function getRandomUserAgent() {
    const userAgents = [
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
        'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Mobile/15E148 Safari/604.1',
        'Mozilla/5.0 (iPad; CPU OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Mobile/15E148 Safari/604.1',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:90.0) Gecko/20100101 Firefox/90.0'
    ];
    return userAgents[Math.floor(Math.random() * userAgents.length)];
}

// 设置基本请求头
export function getHeaders(referer = null) {
    const headers = {
        'User-Agent': getRandomUserAgent(),
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'Cache-Control': 'max-age=0',
        'sec-ch-ua': '"Google Chrome";v="91", "Chromium";v="91", ";Not A Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': referer ? 'same-origin' : 'none',
        'Sec-Fetch-User': '?1'
    };

    // 如果有referer，添加到请求头
    if (referer) {
        headers['Referer'] = referer;
    }

    return headers;
}

// 网站特定的解析配置
const SITE_PARSERS = {
    // 3yt.org 网站配置
    '3yt.org': {
        contentSelectors: [
            '.read-content',
            '.article-content',
            '#content'
        ],
        nextPageSelectors: [
            'a:contains("下一页")',
            '.page a.next',
            '.pagination a:contains("下一页")'
        ],
        paginationSelector: '.pagination, .page-nav, .page-links',
        activePageSelector: '.active, .current',
        cleanupPatterns: [
            /\[笔趣阁\]/g,
            /\(三月天\)/g,
            /手机阅读本章.*/,
            /https?:\/\/[^\s\n]+/g,
            /[a-zA-Z\d]{10,}/g,
            /推荐都市大神.*/,
            /最新章节.*/,
            /本章未完.*/
        ]
    },
    // 笔趣阁网站配置
    'xbiquge.la': {
        contentSelectors: [
            '#content',
            '.showtxt'
        ],
        nextPageSelectors: [
            'a:contains("下一页")',
            'a:contains("下一章")'
        ],
        paginationSelector: '.bottem2',
        activePageSelector: '.this',
        cleanupPatterns: [
            /笔趣阁|www\.xbiquge\.la/g,
            /章节目录|加入书签|推荐本书|返回目录|返回书页|上一页|下一页|上一章|下一章/g,
            /请记住本站域名|手机阅读|最新章节|全文阅读|更新时间/g
        ]
    },
    // 默认配置（通用）
    'default': {
        contentSelectors: [
            '#content',
            '.read-content',
            '.article-content',
            '#chaptercontent',
            '.content',
            'article',
            '.chapter-content',
            '.txt',
            '.showtxt'
        ],
        nextPageSelectors: [
            'a:contains("下一页")',
            'a:contains("下一章")',
            'a:contains("下一頁")',
            'a:contains("Next")'
        ],
        paginationSelector: '.pagination, .page-nav, .page-links, .bottem, .bottem2',
        activePageSelector: '.active, .current, .this',
        cleanupPatterns: [
            /\[笔趣阁\]/g,
            /\(三月天\)/g,
            /手机阅读本章.*/,
            /https?:\/\/[^\s\n]+/g,
            /[a-zA-Z\d]{10,}/g,
            /推荐都市大神.*/,
            /最新章节.*/,
            /本章未完.*/,
            /章节目录|加入书签|推荐本书|返回目录|返回书页|上一页|下一页|上一章|下一章/g,
            /请记住本站域名|手机阅读|手机阅读|最新章节|全文阅读|更新时间/g
        ]
    }
};

// 获取网站解析配置
function getSiteParser(url) {
    const urlObj = new URL(url);
    const hostname = urlObj.hostname.replace(/^www\./, '');

    // 检查是否有匹配的网站配置
    for (const site in SITE_PARSERS) {
        if (hostname.includes(site)) {
            console.log(`使用 ${site} 的解析配置`);
            return SITE_PARSERS[site];
        }
    }

    // 如果没有匹配的配置，使用默认配置
    console.log('未找到匹配的解析配置，使用默认配置');
    return SITE_PARSERS.default;
}

// 清理文本内容
function cleanText(text, cleanupPatterns) {
    if (!text) return '';

    let cleanedText = text
        .replace(/\s+/g, '\n')  // 将连续空白字符替换为换行
        .replace(/\n+/g, '\n'); // 将连续换行替换为单个换行

    // 应用网站特定的清理模式
    if (cleanupPatterns && Array.isArray(cleanupPatterns)) {
        for (const pattern of cleanupPatterns) {
            cleanedText = cleanedText.replace(pattern, '');
        }
    }

    return cleanedText
        .replace(/\n+/g, '\n')  // 再次替换连续换行
        .trim();  // 移除首尾空白
}

// 解析章节内容
export async function parseChapter(url, delay = 1000) {
    try {
        // 获取网站特定的解析配置
        const parser = getSiteParser(url);

        let allContent = '';
        let currentUrl = url;
        let pageCount = 0;
        const maxPages = 20; // 防止无限循环的安全限制
        let referer = null; // 初始没有referer

        while (currentUrl && pageCount < maxPages) {
            pageCount++;
            console.log(`正在获取章节页面 ${pageCount}: ${currentUrl}`);

            // 添加随机延迟，避免请求过于规律
            if (pageCount > 1) {
                const randomDelay = delay + Math.floor(Math.random() * 500);
                await new Promise(resolve => setTimeout(resolve, randomDelay));
            }

            try {
                // 使用当前referer获取请求头
                const currentHeaders = getHeaders(referer);

                // 发送请求
                const response = await axios.get(currentUrl, {
                    headers: currentHeaders,
                    timeout: 10000, // 10秒超时
                    maxRedirects: 5
                });

                // 更新referer为当前URL，用于下一次请求
                referer = currentUrl;

                const $ = cheerio.load(response.data);

                // 获取章节内容，尝试网站特定的选择器
                let content = '';
                for (const selector of parser.contentSelectors) {
                    const element = $(selector);
                    if (element.length > 0) {
                        content = element.text();
                        if (content.trim()) {
                            break;
                        }
                    }
                }

                // 如果没有找到内容，尝试查找所有可能包含内容的元素
                if (!content.trim()) {
                    // 尝试查找包含大量文本的div或p元素
                    $('div, p').each(function () {
                        const text = $(this).text().trim();
                        if (text.length > 500) { // 假设正文至少有500个字符
                            content = text;
                            return false; // 跳出each循环
                        }
                    });
                }

                // 清理内容并添加到总内容
                content = cleanText(content, parser.cleanupPatterns);
                if (content) {
                    allContent += content + '\n\n';
                }

                // 检查是否有下一页
                let nextPageUrl = null;

                // 尝试使用网站特定的下一页选择器
                for (const selector of parser.nextPageSelectors) {
                    const element = $(selector);
                    if (element.length > 0) {
                        const href = element.attr('href');
                        if (href && !href.includes('javascript')) {
                            nextPageUrl = new URL(href, currentUrl).href;
                            break;
                        }
                    }
                }

                // 如果没有找到明确的下一页链接，尝试查找分页导航
                if (!nextPageUrl) {
                    const pagination = $(parser.paginationSelector);
                    if (pagination.length > 0) {
                        const currentActive = pagination.find(parser.activePageSelector);
                        if (currentActive.length > 0) {
                            const nextPage = currentActive.next('a');
                            if (nextPage.length > 0) {
                                const href = nextPage.attr('href');
                                if (href) {
                                    nextPageUrl = new URL(href, currentUrl).href;
                                }
                            }
                        }
                    }
                }

                // 更新当前URL为下一页URL
                currentUrl = nextPageUrl;

            } catch (requestError) {
                console.error(`请求页面失败: ${currentUrl}`, requestError.message);

                // 如果是最后一次尝试，则抛出错误
                if (pageCount >= 3) {
                    throw requestError;
                }

                // 否则，增加延迟后重试
                console.log(`等待 ${delay * 2}ms 后重试...`);
                await new Promise(resolve => setTimeout(resolve, delay * 2));
            }
        }

        // 如果内容为空，可能是反爬措施，抛出错误
        if (!allContent.trim()) {
            throw new Error('无法获取章节内容，可能是反爬措施');
        }

        return allContent.trim();
    } catch (error) {
        console.error('解析章节失败:', error.message);
        throw error;
    }
}